package android.labis.actividades;

import android.app.ListActivity;
import android.content.Intent;
import android.labis.R;
import android.labis.aplicacion;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;



public class CursosTab extends ListActivity implements OnItemClickListener{
	private aplicacion globales;
	String[] ids;
	String[] cursos;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		String[] result;
		try {
	        super.onCreate(savedInstanceState);
	        globales = (aplicacion)getApplication();
			result = globales.getConexion().GetCursos();
	    	String rest = ""; 
	    	ids = new String[result.length / 2];
	    	cursos = new String[result.length / 2];
	    	for (int i = 0; i < result.length; i+=2) {
	    		rest += "Id: [" + result[i] + "]\nCurso: [" + result[i+1] + "]\n";
	    		ids[i/2] = result[i];
	    		cursos[i/2] = result[i+1];
	    	}

	
			setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, cursos));

			  ListView lv = getListView();
			  lv.setTextFilterEnabled(true);

			  lv.setOnItemClickListener(this);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Toast.makeText(getApplicationContext(), getString(R.string.connection_error),
			          Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
	}
	
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// When clicked, show a toast with the TextView text
	    //  Toast.makeText(getApplicationContext(), ((TextView) view).getText()+" "+IDS[position],
	    //      Toast.LENGTH_SHORT).show();
	      
		
		
		  globales.setIdcurso(ids[position]);
		  globales.setNombrecurso(cursos[position]);
		  
		  
		  
		  
	      Toast.makeText(getApplicationContext(), getString(R.string.accesing_to)+ " "+getString(R.string.course)+ " :" + cursos[position],
		          Toast.LENGTH_SHORT).show();
		      
	       
	      Intent intent = new Intent(this, ActividadesTab.class);
	  		startActivity(intent);
			   
	      
	
	}
	
}

